import { Strategy as JwtStrategy, ExtractJwt } from 'passport-jwt';
import * as UserService from '../api/services/user.service';
import {SECRET_KEY} from "./constants";

const opts: any = {};
opts.jwtFromRequest = ExtractJwt.fromAuthHeaderAsBearerToken();
opts.secretOrKey = SECRET_KEY;

export default (passport: any) => {
  passport.use(new JwtStrategy(opts, async (jwt_payload: any, done: any) => {
    // console.log(jwt_payload);
    await UserService.getUserById(jwt_payload.id)
        .then((user: any) => {
          if(user){
            return done(null,user);
          }

          return done(null,false);
        })
        .catch((err: any) => console.log(err));
  }));
}